package com.example.liuliang.broadnotifytest210;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

/**
 * Created by liuliang on 2017/5/3.
 */

public class MyReceiver extends BroadcastReceiver {

    private Context context;
    private String str="";

    public static NotificationManager manager;
    public static int notifyID=1;
    @Override
    public void onReceive(Context context, Intent intent) {
        this.context=context;
        str=intent.getStringExtra("para2");
        Toast.makeText(context,"接受到广播:"+str,Toast.LENGTH_SHORT);
    }
    private void sendnotify() {

       manager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builer=new NotificationCompat.Builder(context);
        Intent intent=new Intent(context,NotifyActivity.class);
        intent.putExtra("para2",str);
        PendingIntent pintent=PendingIntent.getActivity(context,notifyID,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        builer.setContentTitle("通知的标题")
                .setContentText("通知的内容:"+str)//通知内容
                .setSmallIcon(R.mipmap.ic_launcher)//显示在通知栏上的小图标
                .setTicker("通知来了")//通知栏上面显示的文字// 
                .setContentIntent(pintent)//设置通知栏点击意图
                .setDefaults(Notification.DEFAULT_ALL)//使用defaults属性
                .setAutoCancel(true)//设置这个标志当单击面板就可以让通知将自动取消
                .setWhen(System.currentTimeMillis());//通知产生的时间
        manager.notify(notifyID,builer.build());}

}
